/**
 *  Copyright (c) 2011, IPC Studio, USTC, All rights reserved
 *  FileName: CmdParam.cpp
 *  Author: chengximing1989@gmail.com
 *  DateCreated: 2011-8-8
 */

#include "../include/CmdParam.h"
#include "../include/Log.h"

CmdParam::CmdParam()
{
	m_sifilename = "";
	m_sofilename = "";
	m_bprintheadinfo = false;
	m_bprintall = false;
	m_bprinthelp = false;
	m_bprintversion = false;
	m_itime = -1;
}

CmdParam::~CmdParam()
{
}

bool CmdParam::InitParam(int argc, char** argv)
{
	if(argc < 2)
	{
		//cout<<"No input Command(s) found, use -h for help"<<endl;
		/*liblyrics::CLog::LogDefault(LOG_ERROR, "No input Command(s) found, use -h for help\n");
		return false;*/
		argv[1] = "-v";
		argv[2] = "-h";
		argc = 3;
	}
	if(!ParseParam(argc, argv))
		return false;

	return true;
}

bool CmdParam::ParseParam(int argc, char** argv)
{
	for(int i = 1; i < argc; i++)
	{
		string cmdtxt = argv[i];
		if(cmdtxt == "-i")
		{
			if((++i < argc)&&(argv[i][0] != 45))	//NOT "-", "-" ASCII is 45
				m_sifilename = argv[i];
			else
			{
				//cout<<"Warning : -i command needs file name/path input!"<<endl;
				liblyrics::CLog::LogDefault(LOG_WARNING, "-i command needs file name/path input!\n");
				i--;
			}
		}
		else if(cmdtxt == "-t")
		{
			if((++i < argc)&&(argv[i][0] != 45))
				m_itime = atoi(argv[i]);
			else
			{
				//cout<<"Warning : -t command needs time input!"<<endl;
				liblyrics::CLog::LogDefault(LOG_WARNING, "-t command needs time input!\n");
				i--;
			}
		}
		else if(cmdtxt == "-o")
		{
			if((++i < argc)&&(argv[i][0] != 45))
				m_sofilename = argv[i];
			else
			{
				//cout<<"Warning : -o command needs file name/path output!"<<endl;
				liblyrics::CLog::LogDefault(LOG_WARNING, "-o command needs file name/path output!\n");
				i--;
			}
		}
		else if(cmdtxt == "-H")
			m_bprintheadinfo = true;
		else if(cmdtxt == "-a")
			m_bprintall = true;
		else if(cmdtxt == "-h")
			m_bprinthelp = true;
		else if(cmdtxt == "-v")
			m_bprintversion = true;
		else
		{
			//cout<<"Error input param(s), use -h for help"<<endl;
			liblyrics::CLog::LogDefault(LOG_ERROR, "Error input param(s), use -h for help\n");
			return false;
		}
	}
	return true;
}
